let app = getApp();
import { getVideoById } from "../../../../request/api";

Page({

    /**
     * 页面的初始数据
     */
    data: {
        lang: null,
        userInfo: '',
        videoList: [],
        favoriteCount: 0,
        navigationBarHeight: wx.getStorageSync('navigationBarHeight') + 'px',
        
        // 分页
        pageNo: 1,
        totalPage: 0,
    },

    // 获取内容
    getPost() {
        let userId = this.data.userInfo.id
        let data = { userId, pageNo : this.data.pageNo }
        getVideoById(res => {
            if (res.statusCode == 200) {
                if (res.data && typeof res.data.data != "undefined" && res.data.data.length > 0) {
                    let videosInCurrPage = res.data.data
                    for (let item of videosInCurrPage) {
                        item["pageNo"] = this.data.pageNo
                    }
                    // console.log("videosInCurrPage: ", videosInCurrPage)
                }
                this.setData({ videoList: this.data.videoList.concat(res.data.data), totalPage : res.data.totalPage, favoriteCount: res.data.favorite_count[0].FC })
            }
        }, data);
    },

    // 返回
    previous() {
        wx.navigateBack({
            delta: 1,
            fail: err => {
                console.log("err: ", err);
                wx.navigateTo({
                  url: '/pages/index/index',
                })
            }
        })
    },

    // 个人视频
    personalVideo(e) {
        let videoId = e.currentTarget.dataset.item.id
        let index = e.currentTarget.dataset.index
        let userId = this.data.userInfo.id
        let pageNo = e.currentTarget.dataset.item.pageNo
        let data = { videoId, userId, index, pageNo }
        let json = JSON.stringify(data);

        wx.navigateTo({
            url: '/pages/home/video/personalVideo/personalVideo?json=' + json
        });
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let value = JSON.parse(options.json)
        this.setData({ lang: app.globalData.lang, userInfo: value })
        this.getPost()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if(this.data.pageNo == this.data.totalPage) return
        let pageNo = this.data.pageNo += 1
        this.setData({pageNo : pageNo})
        this.getPost();
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})